package com.kuaimacode.kframework.agent.controller.order;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.services.order.OrderEvaluateService;
import com.kuaimacode.kframework.agent.services.order.OrderService;
import com.kuaimacode.kframework.agent.services.user.UserService;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.component.ComponentUtil;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.user.Order;
import com.kuaimacode.kframework.mybatis.models.user.OrderEvaluate;
import com.kuaimacode.kframework.mybatis.models.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author 中元
 * @date 2019/04/08
 */
/**
 * Controller
 */
@Controller
@RequestMapping({"/orderEvaluate"})
public class OrderEvaluateController {
	private static String PAGE_FILE_NAME = "orderEvaluate/";

	@Autowired
	private OrderEvaluateService orderEvaluateServices;
	@Autowired
	private UserService userService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private ComponentUtil componentUtil;



	@RequestMapping(value = {"/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						ModelMap model) {
		PageInfo<OrderEvaluate> pageInfo = orderEvaluateServices.findAll(pageNum, pageSize);
		// pageInfo.list orderEvaluate
		model.put("pageInfo", pageInfo);
		return PAGE_FILE_NAME + "index";
	}


	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap map) {
		OrderEvaluate orderEvaluate = new OrderEvaluate();
		map.put("orderEvaluate", orderEvaluate);
		map.put("isAdd", true);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		OrderEvaluate orderEvaluate = orderEvaluateServices.getById(id);
		map.put("orderEvaluate", orderEvaluate);
		map.put("isAdd", false);
		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(OrderEvaluate orderEvaluate, Boolean isAdd,ModelMap map) {
		try {
			if(isAdd){
				// 新建保存
				orderEvaluateServices.save(orderEvaluate);
			}else{
				// 编辑保存
				orderEvaluateServices.updateById(orderEvaluate);
			}
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


	@RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
	public String detail(@PathVariable String id, ModelMap map) {
		OrderEvaluate orderEvaluate = orderEvaluateServices.getById(id);
		if(orderEvaluate != null ){
			getDetail(orderEvaluate, map);
		}
		return PAGE_FILE_NAME+"detail";
	}

	/**
	 * 根据订单ID查询评价
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/detailByOrderId/{id}", method = RequestMethod.GET)
	public String detailByOrderId(@PathVariable Long id, ModelMap map) {
		OrderEvaluate orderEvaluate = orderEvaluateServices.getByOrderId(id);
		if(orderEvaluate != null ){
			getDetail(orderEvaluate, map);
		}
		return PAGE_FILE_NAME+"detail";
	}

	/**
	 * 获取详情数据
	 * @param orderEvaluate
	 * @param map
	 * @return
	 */
	private void getDetail(OrderEvaluate orderEvaluate, ModelMap map){
		User user = userService.getById(orderEvaluate.getUserId());
		User master = userService.getById(orderEvaluate.getMasterId());
		Order order = orderService.getById(orderEvaluate.getOrderId());
		//处理图片
		List<String> images = new ArrayList<String>();
		Arrays.stream(Optional.ofNullable(orderEvaluate.getImages()).get().split(",")).forEach(o->{
			images.add(componentUtil.convertImgUrl(o));
		});
		//返回数据
		map.put("orderEvaluate", orderEvaluate);
		map.put("images", images);
		map.put("user", user);
		map.put("master", master);
		map.put("order", order);
	}

	@ResponseBody
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		try {
			orderEvaluateServices.removeById(id);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}


}
